<?php

namespace App\Kuafu\User\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\User\Model\UserRegistrationMappings;

class UserBindService extends BaseService
{
    //绑定用户与设备
    public static function bindRegistrationId($userId, $registrationId)
    {
        if ($registrationId == null || $registrationId == '') {
            //修改状态
            UserRegistrationMappings::where('userId', $userId)->update(['status' => 0]);
            return [
                'userId'   => $userId,
                'describe' => '已解绑'
            ];
        }
        // 解绑
        UserRegistrationMappings::query()
            ->where('registrationId', $registrationId)
            ->update(['status' => 0]);
        UserRegistrationMappings::query()
            ->where('userId', $userId)
            ->update(['status' => 0]);
        // 绑定
        $m = UserRegistrationMappings::firstOrNew([
            'registrationId' => $registrationId,
            'userId'         => $userId,
        ]);
        $m->status = 1;
        $m->save();
        return [
            'userId'   => $userId,
            'describe' => '已绑定'
        ];
    }
}
